# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from kestrel.worker import Executor, Policy, Interface


class WorkerRole(object):
    """
    The worker role is responsible for accepting and executing
    tasks that meet policy requirements.

    Configuration entries for the modules in this role:
      worker/executor
      worker/policy
      worker/interface

    Events raised by this module:
      worker/start
      worker/end
    """
    def __init__(self):
        """
        Set the name of the role and define the default module
        implementations.
        """
        self.name = 'worker'
        self.defaults = {'worker/executor': Executor,
                         'worker/policy': Policy,
                         'worker/interface': Interface}
        self.modules = {}

    def register(self, kernel):
        """
        Instantiate and register the role's modules. The actual
        module implementations can be swapped by setting the
        appropriate configuration entries.
        """
        for module in self.defaults:
            impl = kernel.config(module, self.defaults[module])
            self.modules[module] = impl(kernel)

        kernel.event('worker/start')

    def unregister(self, kernel):
        """Unregister the role's modules from the kernel."""
        kernel.event('worker/end')
